<?php
/*
Plugin Name: VTP Clients Custom User Meta
Plugin URI: http://www.dudyland.sk
Description: VTP Clients extra data
Author: Peter Turza, Pavol Martiniak
Version: 0.1
Author URI: http://www.dudyland.sk

Add new custom user meta fields to the user's Profile in the WordPress Dashboard.

*/

class VTPClients {

    var $types = array(1=>'Klient z inkubátora', 2=>'Klient technologického parku');

    function __construct() {
        add_action('user_register', array(&$this, 'create_category_for_client'), 10, 3);
        if ( is_admin() ) {
            // ak je v admin rozhrani
            // pridaj akcie
            add_action('show_user_profile', array(&$this,'action_show_user_profile'));
            add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
            add_action('personal_options_update', array(&$this,'action_process_option_update'));
            add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
            add_action('save_post', array(&$this,'add_category_to_post'), 10, 3);

            // registruje skript
            wp_register_script('jquery-validate', get_bloginfo(url).'/wp-includes/js/validate/jquery.validate.js');            
        }
    }
    
    function create_category_for_client($uid) {
        $userData = get_userdata($uid);
        if ($userData->wp_user_level == 2) {
            // ak je to vtp klient
            // vytvor novu kategoriu zatial s nazvom noveho clienta
            $newCat = array('cat_name'=>$userData->user_nicename);
            // vloz ju
            $newCatId = wp_insert_category($newCat);
            // uloz informacie od usermeta
            update_usermeta($uid, 'client_category', $newCatId);
            // pridel prava na pridavanie postov do svojej kategorie pre klienta
            UserCatsManager::save($uid, array($newCatId));
            
        }
    }

    function add_category_to_post($post_id) {
        global $user_ID;
        $userData = get_userdata($user_ID);
        if ($userData->wp_user_level == 2) {
            wp_set_post_categories($post_id, UserCatsManager::getUserCategories($user_ID));
        }
    }

    function action_show_user_profile() {
        // nacita skript
        wp_enqueue_script('jquery-validate');        
        global $profileuser;
        // id usera, ktoreho profil sa edituje
        $user_id = $profileuser->ID;
        
        echo $_POST[''];
        
        // ak je to author / vtp klient
        if (get_usermeta($user_id, 'user_level') == 2) {
            ?>
<h3><?php _e('VTP Klient') ?></h3>

<table class="form-table">
    <tr>
        <th><label for="name"><?php _e('Názov');?></label></th>
        <td><input type="text" name="name" id="nameMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'name') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="activity"><?php _e('Činnosť');?></label></th>
        <td><textarea cols="20" rows="10" name="activity" id="activityMeta" class="regular-text"><?php echo esc_attr(get_usermeta($user_id, 'activity') );?></textarea></td>
    </tr>
    <tr>
        <th><label for="where"><?php _e('Umiestnenie');?></label></th>
        <td>
            <select name="where" id="whereMeta">
            <?php 
                foreach ($this->types as $k=>$v) {
            ?>

                <option value="<?php echo $k ?>" <?php echo esc_attr(get_usermeta($user_id, 'where') )==$k?'selected="selected"':'' ?>><?php echo $v ?></option>
            <?php } ?>
            </select>
        </td>
    </tr>
    <tr>
        <th><label for="icq"><?php _e('ICQ');?></label></th>
        <td><input type="text" name="icq" id="icqMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'icq') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="skype"><?php _e('Skype');?></label></th>
        <td><input type="text" name="skype" id="skypeMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'skype') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="phone1"><?php _e('Telefón 1');?></label></th>
        <td><input type="text" name="phone1" id="phone1Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone1') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="phone2"><?php _e('Telefón 2');?></label></th>
        <td><input type="text" name="phone2" id="phone2Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone2') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="phone3"><?php _e('Telefón 3');?></label></th>
        <td><input type="text" name="phone3" id="phone3Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone3') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="fax"><?php _e('Fax');?></label></th>
        <td><input type="text" name="fax" id="faxMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'fax') );?>" class="regular-text"/></td>
    </tr>
</table>
<script type="text/javascript">
    var userPhotoValidation;
    if (document.getElementById("userphoto_delete") == null) {
        userPhotoValidation = {required: true, accept: "jpe?g|gif|png"};
    } else {
        userPhotoValidation = {accept: "jpe?g|gif|png"};
    }
    jQuery("#your-profile").validate({
        rules: {
            first_name: {required: true},
            last_name: {required: true},
            nickname: {required: true},
            email: {required: true, email: true},
            url: {url: true},
            userphoto_image_file: userPhotoValidation,
            phone1: {},
            phone2: {},
            phone3: {},
            fax: {},
            name: {required: true},
            activity: {required: true}
            
        }
    });
</script>
            <?php
        }
    }

    // ulozi meta informacie
    function action_process_option_update() {
        // zisti ktoreho usera sme upravovali
        $user_id = $_POST['user_id'];
        update_usermeta($user_id, 'name', ( isset($_POST['name']) ? $_POST['name'] : '' ) );
        update_usermeta($user_id, 'where', ( isset($_POST['where']) ? $_POST['where'] : '' ) );
        update_usermeta($user_id, 'activity', ( isset($_POST['activity']) ? $_POST['activity'] : '' ) );
        update_usermeta($user_id, 'icq', ( isset($_POST['icq']) ? $_POST['icq'] : '' ) );
        update_usermeta($user_id, 'skype', ( isset($_POST['skype']) ? $_POST['skype'] : '' ) );
        update_usermeta($user_id, 'phone1', ( isset($_POST['phone1']) ? $_POST['phone1'] : '' ) );
        update_usermeta($user_id, 'phone2', ( isset($_POST['phone2']) ? $_POST['phone2'] : '' ) );
        update_usermeta($user_id, 'phone3', ( isset($_POST['phone3']) ? $_POST['phone3'] : '' ) );
        update_usermeta($user_id, 'fax', ( isset($_POST['fax']) ? $_POST['fax'] : '' ) );
        $updateCat = array('cat_ID' => get_usermeta($user_id, 'client_category'),'cat_name'=> get_usermeta($user_id, 'name'));
        wp_insert_category($updateCat);
    }

    function getClients($type,$sort = 'user_nicename') {
        global $wpdb;
        $userIds = $wpdb->get_col( $wpdb->prepare(
            "SELECT sel1.id id FROM
            (SELECT distinct $wpdb->users.ID id FROM $wpdb->users
            LEFT JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id)
            WHERE $wpdb->usermeta.meta_key = 'wp_user_level'
            AND $wpdb->usermeta.meta_value = 2
            ORDER BY %s ASC) as sel1
            LEFT JOIN $wpdb->usermeta ON (sel1.id = $wpdb->usermeta.user_id)
            WHERE $wpdb->usermeta.meta_key = 'where'
            AND $wpdb->usermeta.meta_value = %s"
            , $sort, $type));
        $result = array();
        foreach ($userIds as $uid) {
            $result[] = get_userdata($uid);
        }
        return $result;
    }

    function getPhoto($uid, $thumbnail=true) {
        if (userphoto_exists($uid)) {
            if ($thumbnail) {
                echo userphoto__get_userphoto($uid, USERPHOTO_THUMBNAIL_SIZE, '', '', array(), '');
            } else {
                echo userphoto__get_userphoto($uid, USERPHOTO_FULL_SIZE, '', '', array(), '');
            }
        } else {
            echo '<img src="'.get_bloginfo(url).'/wp-content/plugins/nophoto.png" />';
        }
    }

}

/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $vtpClients; $vtpClients = new VTPClients();'));


?>
